package com.xmg.p2p.base.service.impl;

import com.xmg.p2p.base.domain.Logininfo;
import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.domain.VideoAuth;
import com.xmg.p2p.base.mapper.VideoAuthMapper;
import com.xmg.p2p.base.query.PageResult;
import com.xmg.p2p.base.query.VideoAuthQO;
import com.xmg.p2p.base.service.IUserinfoService;
import com.xmg.p2p.base.service.IVideoAuthService;
import com.xmg.p2p.base.util.BitStatesUtils;
import com.xmg.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class VideoAuthServiceImpl implements IVideoAuthService{
    @Autowired
    private VideoAuthMapper videoAuthMapper;
    @Autowired
    private IUserinfoService userinfoService;
    @Override
    public PageResult query(VideoAuthQO qo) {
        int count = videoAuthMapper.queryCount(qo);
        if(count>0){
            return new PageResult(videoAuthMapper.query(qo),count,qo.getCurrentPage(),qo.getPageSize());
        }
        return PageResult.empty(qo.getPageSize());
    }

    @Override
    public void audit(Long loginInfoValue, int state, String remark) {
        Userinfo user = userinfoService.get(loginInfoValue);
        if(user!=null&&!user.getIsVideoAuth()){
            VideoAuth va = new VideoAuth();
            Logininfo applier = new Logininfo();
            applier.setId(loginInfoValue);
            va.setApplier(applier);
            va.setApplyTime(new Date());
            va.setAuditor(UserContext.getCurrent());
            va.setAuditTime(new Date());
            va.setState(state);
            va.setRemark(remark);
            videoAuthMapper.insert(va);
            if(state==VideoAuth.STATE_PASS){
                user.addState(BitStatesUtils.OP_VIDEO_AUTH);
                userinfoService.update(user);
            }
        }
    }
}
